<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>catch</title>
</head>

<body>

</body>
<script>
    function rebuy() {
        console.log("开始买笔");
        var p = new Promise(function (resolve, reject) {
            setTimeout(function () {
                console.log("买笔成功");
                resolve("数学作业");
            }, 1000);

            // setTimeout(function () {
            //     console.log("买笔失败");
            //     reject("没带够钱");
            // }, 1000);

        });
        return p;
    }
    function rework(data) {
        if(data == "数学作业") {
            throw new Error("买了坏笔芯")
        }
        console.log("开始写作业：" + data);
        var p = new Promise(function (resolve, reject) {
            setTimeout(function () {
                console.log("写完作业");
                resolve("作业本");
            }, 1000);
        }); return p;
    }
    rebuy().then(rework).catch(function (data) { console.log(data); });

    // 1.catch对上面出现的第一个reject进行处理并终止链式查询
    // 2.catch对resolve的回调，如果抛出异常，也不会卡死js，而是会进到这个catch方法中
    

</script>

</html>